package Test;

/**
 * Created with IntelliJ IDEA.
 * User: happyiu
 * Date: 2022-01-18
 * Time: 7:16
 * Description:
 */
public class TestDemo1 {
    public static void main1(String[] args) {
        String string = "ababcabcd";
        String str = "abc";
        int index = string.indexOf(str);
        System.out.println(index);
    }

    public static void main2(String[] args) {
        String string = "ababcabcd";
        String str = "abc";
        int index = string.indexOf(str,3);
        System.out.println(index);
    }

    public static void main3(String[] args) {

        String str = "ababcabcd";
        String tmp = "abc";

        System.out.println(str.lastIndexOf(tmp));
    }

    public static void main4(String[] args) {
        String str = "ababcabcd";
        String tmp = "abc";
        System.out.println(str.lastIndexOf(tmp,6));
    }

    public static void main5(String[] args) {
        String str = "ababcabcd";
        String tmp = "abc";
        System.out.println(str.startsWith("a"));
        System.out.println("==================");
        System.out.println(str.startsWith("ac"));
    }

    public static void main6(String[] args) {
        String str = "ababcabcd";
        String tmp = "abc";
        System.out.println(str.startsWith("c",4));
    }

    public static void main7(String[] args) {
        String str = "ababcabcd";
        String tmp = "abc";
        System.out.println(str.endsWith("cd"));
    }

    public static void main8(String[] args) {
        String str = "ababcabcdabcde";
        str.replace('a','g');
        System.out.println(str);

    }

    public static void main9(String[] args) {
        String str = "ababcabcdabcde";
        String ret = str.replace('a','g');
        System.out.println(ret);
        //返回gbgbcgbcdgbcde
    }

    public static void main10(String[] args) {
        String str = "ababcabcdabcde";
        String ret = str.replace('a','g');
        System.out.println(ret); //返回gbgbcgbcdgbcde
        System.out.println("====================");

        String ret2 = str.replace("ab","hh");
        System.out.println(ret2);//返回hhhhchhcdhhcde
        System.out.println("====================");

        String ret3 = str.replaceAll("ab","gg");
        System.out.println(ret3);//返回ggggcggcdggcde
        System.out.println("====================");

        String ret4 = str.replaceFirst("ab","gg");
        System.out.println(ret4);//返回ggabcabcdabcde

    }

    public static void main11(String[] args) {
        String str = "name=zhangsan&age=19";
        String[] strings = str.split("&");
        for(String s:strings){
            System.out.println(s);
        }
    }

    public static void main12(String[] args) {
        String str = "192.168.1.1";
        String[] strings = str.split("\\.",2);
        for (String s:strings) {
            System.out.println(s);
        }
    }

    public static void main13(String[] args) {
        String str = "zhangsan&hhh#123";
        String[] strings = str.split("&|#");
        for (String s:strings) {
            System.out.println(s);
        }
    }


    public static void main14(String[] args) {
        String str = "zhangsan&hhh#123";
        String sub = str.substring(2);
        System.out.println(sub);
    }

    public static void main15(String[] args) {
        String str = "zhangsan&hhh#123";
        String sub = str.substring(2,5);
        System.out.println(sub);
    }

    public static void main16(String[] args) {
        String str ="     abc    de    ";
        String ret =str.trim();
        System.out.print("字符串前后");
        System.out.print(ret);
        System.out.println("是连续的，没有空格");
    }

    public static void main17(String[] args) {
        String str ="abcedeFG123张三";
        String ret = str.toUpperCase();
        System.out.println(ret);
    }


    public static void main18(String[] args) {
        String str ="ABCDEfg123张三";
        String ret = str.toLowerCase();
        System.out.println(ret);
        //打印abcdefg123张三
    }

    public static void main19(String[] args) {
        //代码基于JDK 8

    //s1指向字符串常量池中的"我是张三"
        String s1 = "我是张三";
    //s2也指向字符串常量池中的"我是张三"
        String s2 = "我是张三";
    //s3指向堆中的某个对象
        String s3 = new String("我是张三");
    //因为字符串常量池中已经存在"我是张三"的引用，直接返回这个引用
        String s4 = s3.intern();

    //创建一个字符串对象
        String s5 = new String("hhh");
    //常量池中不存在指向"hhh"的引用，创建一个"hhh"对象，并将其引用存入常量池
        String s6 = s5.intern();
    //创建一个字符串对象
        String s7 = new String("hhh");
    //常量池中存在指向"hhh"的引用，直接返回
        String s8 = s7.intern();
    }

    public static void main20(String[] args) {
        String str = "aaaaa";
        String ret =str.concat("bbb");
        System.out.println(ret);
        //打印aaaaabbb
    }

    public static void main21(String[] args) {
        int [] array = {1,2,3,4,5,6};
        String str = "abcde";
        System.out.println(array.length);//6
        System.out.println(str.length());//5
    }

    public static void main22(String[] args) {
        String str1 = "";
        String str2 = "    ";
        String str3 = "123456";

        System.out.println(str1.isEmpty());
        System.out.println(str2.isEmpty());
        System.out.println(str3.isEmpty());
    }

}
